package party.loveit.download.core;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.io.File;
import java.util.List;
import java.util.Vector;
import party.loveit.download.listener.IDListener;


/**
 * Created by MRKING on 2017/1/4.
 */

public class DLListenerMgr {

    private volatile static DLListenerMgr instance;

    private DLListenerMgr() {
    }

    private static final List<IDListener> DL_LISTENERS = new Vector<>();
    private static final String EXTRA_ARGS_1 = "extra_args_1";
    private static final String EXTRA_ARGS_2 = "extra_args_2";
    private static final String EXTRA_ARGS_3 = "extra_args_3";


    private static final int START_EVENT    = 0;
    private static final int PROGRESS_EVENT = START_EVENT + 1;
    private static final int STOP_EVENT     = START_EVENT + 2;
    private static final int FINISH_EVENT   = START_EVENT + 3;
    private static final int ERROR_EVENT    = START_EVENT + 4;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            switch (msg.what) {
                case START_EVENT:
                    notifiStart(bundle.getString(EXTRA_ARGS_1), bundle.getString(EXTRA_ARGS_2), bundle.getLong(EXTRA_ARGS_3));
                    break;
                case PROGRESS_EVENT:
                    notifiProgress(bundle.getString(EXTRA_ARGS_1), bundle.getString(EXTRA_ARGS_2), bundle.getLong(EXTRA_ARGS_3));
                    break;
                case STOP_EVENT:
                    notifiStop(bundle.getString(EXTRA_ARGS_1), bundle.getLong(EXTRA_ARGS_2));
                    break;
                case FINISH_EVENT:
                    notifiFinish(bundle.getString(EXTRA_ARGS_1), (File) bundle.getSerializable(EXTRA_ARGS_2));
                    break;
                case ERROR_EVENT:
                    notifiError(bundle.getString(EXTRA_ARGS_1), bundle.getInt(EXTRA_ARGS_2), bundle.getString(EXTRA_ARGS_3));
                    break;
            }
        }
    };


    public static DLListenerMgr getInstance() {
        if (instance == null) {
            synchronized (DLListenerMgr.class) {
                if (instance == null) {
                    instance = new DLListenerMgr();
                }
            }
        }
        return instance;
    }

    void onPrepare(String baseUrl){

    }

    void onStart(String fileName, String baseUrl, long fileLength) {
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_ARGS_1, fileName);
        bundle.putString(EXTRA_ARGS_2, baseUrl);
        bundle.putLong(EXTRA_ARGS_3, fileLength);
        obtainMsg(START_EVENT, bundle);
    }

     void onProgress(String baseUrl, String speed, long progress){
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_ARGS_1, baseUrl);
        bundle.putString(EXTRA_ARGS_2, speed);
        bundle.putLong(EXTRA_ARGS_3, progress);
        obtainMsg(PROGRESS_EVENT, bundle);
    }

    void onStop(String baseUrl, long progress){
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_ARGS_1, baseUrl);
        bundle.putLong(EXTRA_ARGS_2, progress);
        obtainMsg(STOP_EVENT, bundle);
    }

    void onFinish(String baseUrl, File file){
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_ARGS_1, baseUrl);
        bundle.putSerializable(EXTRA_ARGS_2, file);
        obtainMsg(FINISH_EVENT, bundle);
    }

     void onError(String baseUrl, int status, String error){
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_ARGS_1, baseUrl);
        bundle.putInt(EXTRA_ARGS_2, status);
        bundle.putString(EXTRA_ARGS_3, error);
        obtainMsg(ERROR_EVENT, bundle);
    }

    private void obtainMsg(int what, Bundle bundle){
        Message message = Message.obtain(mHandler, what);
        message.setData(bundle);
        mHandler.sendMessage(message);
    }

    private void notifiStart(String fileName, String baseUrl, long fileLength) {
        synchronized (DL_LISTENERS) {
            for (IDListener listener : DL_LISTENERS) {
                listener.onStart(fileName, baseUrl, fileLength);
            }
        }
    }

    private void notifiProgress(String baseUrl, String speed, long progress){
        synchronized (DL_LISTENERS) {
            for (IDListener listener : DL_LISTENERS) {
                listener.onProgress(baseUrl, speed, progress);
            }
        }
    }

    private void notifiStop(String baseUrl, long progress){
        synchronized (DL_LISTENERS) {
            for (IDListener listener : DL_LISTENERS) {
                listener.onStop(baseUrl, progress);
            }
        }
    }

    private void notifiFinish(String baseUrl, File file){
        synchronized (DL_LISTENERS) {
            for (IDListener listener : DL_LISTENERS) {
                listener.onFinish(baseUrl, file);
            }
        }
    }

    private void notifiError(String baseUrl, int status, String error){
        synchronized (DL_LISTENERS) {
            for (IDListener listener : DL_LISTENERS) {
                listener.onError(baseUrl, status, error);
            }
        }
    }

    /**
     * 注册监听
     * @param listener
     */
    public void register(IDListener listener) {
        DL_LISTENERS.add(listener);
    }
    /**
     * 取消注册监听
     * @param listener
     */
    public void unregister(IDListener listener) {
        DL_LISTENERS.remove(listener);
    }

}
